<template>
  <div class="info-page">
    <div class="form-container">
      <el-form
        :model="form"
        ref="formRef"
        label-width="80px"
        class="form"
      >
        <el-form-item label="头像" prop="avatar">
          <el-upload
            :action="uploadPath"
            :before-upload="beforeUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccessAdd"
            :on-error="handleAvatarErrorAdd"
            :headers="{ Authorization: getStorage('TOKEN') }"
            ref="upload"
          >
            <el-avatar
              :src="form.avatar || noAvatar"
              @click="handleAvatarClick"
            />
            <el-input v-model="form.avatar" type="hidden"></el-input>
          </el-upload>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio value="男">男</el-radio>
            <el-radio value="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="form.age" :min="1" :max="120" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色" disabled>
            <el-option label="管理员" value="ADMIN"></el-option>
            <el-option label="普通用户" value="USER"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="editUserInfo">修 改</el-button>
          <el-button type="info" @click="router.back()">返 回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { getCurrentInstance, onMounted, reactive } from 'vue'
import useUserStore from '@/store/user.js'
import router from '@/router/index.js'
import { ElMessage } from 'element-plus'
import { getStorage } from '@/utils/storage.js'
import { getRoleList, updateInfo } from '@/api/User.js'
import noAvatar from '@/assets/no-avatar.png'

const userStore = useUserStore()
const { proxy } = getCurrentInstance()

const BASE_URL = import.meta.env.VITE_BASE_API
const uploadPath = BASE_URL + '/common/upload'

let form = reactive({
  avatar: '',
  nickname: '',
  username: '',
  password: '',
  sex: '男',
  age: '',
  phone: '',
  email: '',
  role: ''
})

const getUserInfo = () => {
  form = userStore.userInfo
}

const editUserInfo = () => {
  updateInfo(form).then(res => {
    if (res.code === 1) {
      ElMessage.success('修改成功!')
      router.go(-1)
    } else {
      ElMessage.error(res.message)
    }
  })
}

const handleAvatarClick = () => {
  proxy.$refs.upload.uploadFiles()
}

// 上传文件前的校验
const beforeUpload = (file) => {
  const isImage = file.type.startsWith('image/')
  const isLt2M = file.size / 1024 / 1024 < 2
  if (!isImage) {
    ElMessage.error('上传文件只能是图片!')
  }
  if (!isLt2M) {
    ElMessage.error('上传图片大小不能超过 2MB!')
  }
  return isImage && isLt2M
}

// 上传图片成功后的回调
const handleAvatarSuccessAdd = (res, file) => {
  form.avatar = file.response.data
}

// 上传图片失败后的回调
const handleAvatarErrorAdd = (err, file) => {
  if (err.toString().indexOf('401')) {
    ElMessage.error('请先登录!')
    router.push('/login')
  } else {
    ElMessage.error(err)
  }
}

onMounted(() => {
  getUserInfo()
})
</script>


<style scoped lang="scss">
.info-page {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  height: 100vh;
  background-color: #f9f9f9;
}

.form-container {
  width: 400px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.avatar {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.form {
  max-width: 400px;
}
</style>
